package com.teacher.hong.servlet;


import com.alibaba.fastjson.JSONObject;
import com.teacher.hong.utils.ServletUtils;
import javax.servlet.*;
import java.io.IOException;
import java.time.LocalDateTime;

public class MyTestServlet implements Servlet {

    ServletConfig config;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("MyTestServlet init...");
        config = servletConfig;
    }

    @Override
    public ServletConfig getServletConfig() {
        return config;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

        System.out.println("service....");
        ServletUtils.recordVisits(getServletConfig().getServletContext());

        /**
         * 操作了一堆我们代码
         */
        JSONObject object = new JSONObject();
        object.put("state", 200);
        object.put("message", "success");
        object.put("timestamp", LocalDateTime.now());

        servletResponse.setContentType("application/json");
        servletResponse.getWriter().write(object.toJSONString());

    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("MyTestServlet destroy...");
    }
}
